﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace SpaceShooter.Fighters
{
    public class Fighter
    {
        private Model mModel;
        private Vector3 mMoveFactor;
        private int textureCounter;
        List<Texture2D> mFlameTex;

        public Fighter()
        {
            mFlameTex = new List<Texture2D>();
            mMoveFactor = Vector3.Zero;
            textureCounter = 1;
        }

        public void Load(string pStrModelName,ContentManager pContent)
        {
            mModel = pContent.Load<Model>(pStrModelName);
            for (int i = 1; i < 10; i++)
            {
                mFlameTex.Add(pContent.Load<Texture2D>("Fighter//Flame//" + i.ToString()));
            }
        }

        public void Update()
        {
            // MoveSpeed 0.1f, Range row(-5,5) col (7,-7)
            if (Keyboard.GetState().IsKeyDown(Keys.Left) && mMoveFactor.X >= -5)
            {
                mMoveFactor.X -= 0.1f;
            }
            if (Keyboard.GetState().IsKeyDown(Keys.Right) && mMoveFactor.X <= 5)
            {
                mMoveFactor.X += 0.1f;
            }
            if (Keyboard.GetState().IsKeyDown(Keys.Up) && mMoveFactor.Z >= -7)
            {
                mMoveFactor.Z -= 0.1f;
            }
            if (Keyboard.GetState().IsKeyDown(Keys.Down) && mMoveFactor.Z <= 7)
            {
                mMoveFactor.Z += 0.1f;
            }
        }

        public void Draw(GraphicsDevice device, Matrix pViewMatrix, Matrix pProjectionMatrix)
        {
            Matrix[] transforms = new Matrix[mModel.Bones.Count];
            mModel.CopyAbsoluteBoneTransformsTo(transforms);
            foreach (ModelMesh mesh in mModel.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.EnableDefaultLighting();
                    effect.World = transforms[mesh.ParentBone.Index] * Matrix.CreateTranslation(mMoveFactor);
                    effect.View = pViewMatrix;
                    effect.Projection = pProjectionMatrix;
                    if (mesh.Name.ToLower() == "flame")
                    {
                        effect.EmissiveColor = Color.Azure.ToVector3();
                        textureCounter++;
                        if (textureCounter == 10)
                        {
                            textureCounter = 1;
                        }
                        effect.Texture = mFlameTex[textureCounter - 1];
                        effect.TextureEnabled = true;

                        device.RenderState.AlphaTestEnable = true;
                        device.RenderState.ReferenceAlpha = 100;
                        device.RenderState.AlphaFunction = CompareFunction.Greater;
                    }
                    else
                    {
                        effect.SpecularColor = Color.White.ToVector3();
                        //Specular power is 25, the lower the value is, the specular will more obvious,(cover whole mesh surface)
                        effect.SpecularPower = 25f;
                    }
                }
                mesh.Draw();
            }
        }
    }
}
